package morenoapp.components.thinkgraphv2.components;

import java.util.ArrayList;
import java.util.List;
import morenoapp.MorenoApp;
import morenoapp.components.thinkgraphv2.components.CheckBoxListTable.DataItem;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.enums.MorenoSex;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.model.view2.MorenoView2Face;

/**
 *
 * @author dmitry.mamonov
 */
public class RespondentDisplayConfigJPanel extends javax.swing.JPanel {
    MorenoView2Face config;

    /** Creates new form RespondentDisplayConfigJPanel */
    public RespondentDisplayConfigJPanel(MorenoView2Face _config) {
        this.config = _config;
        initComponents();

        initComponentsManual();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jCheckBoxShowSex = new javax.swing.JCheckBox();
        jCheckBoxShowNames = new javax.swing.JCheckBox();
        jCheckBoxShowCorrections = new javax.swing.JCheckBox();
        jPanelSplit = new javax.swing.JPanel();
        jPanelSplitLeft = new javax.swing.JPanel();
        jPanelSplitRight = new javax.swing.JPanel();
        jSeparatorCenter = new javax.swing.JSeparator();
        jCheckBoxHideMale = new javax.swing.JCheckBox();
        jScrollPaneLeft = new javax.swing.JScrollPane();
        jCheckBoxHideFemale = new javax.swing.JCheckBox();
        jScrollPaneRight = new javax.swing.JScrollPane();

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(RespondentDisplayConfigJPanel.class);
        jCheckBoxShowSex.setText(resourceMap.getString("jCheckBoxShowSex.text")); // NOI18N
        jCheckBoxShowSex.setName("jCheckBoxShowSex"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${config.displaySex}"), jCheckBoxShowSex, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBoxShowNames.setText(resourceMap.getString("jCheckBoxShowNames.text")); // NOI18N
        jCheckBoxShowNames.setName("jCheckBoxShowNames"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${config.displayNames}"), jCheckBoxShowNames, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBoxShowCorrections.setText(resourceMap.getString("jCheckBoxShowCorrections.text")); // NOI18N
        jCheckBoxShowCorrections.setName("jCheckBoxShowCorrections"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${config.displayCorrections}"), jCheckBoxShowCorrections, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jPanelSplit.setName("jPanelSplit"); // NOI18N
        jPanelSplit.setLayout(new java.awt.GridLayout(1, 2));

        jPanelSplitLeft.setName("jPanelSplitLeft"); // NOI18N

        javax.swing.GroupLayout jPanelSplitLeftLayout = new javax.swing.GroupLayout(jPanelSplitLeft);
        jPanelSplitLeft.setLayout(jPanelSplitLeftLayout);
        jPanelSplitLeftLayout.setHorizontalGroup(
            jPanelSplitLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelSplitLeftLayout.setVerticalGroup(
            jPanelSplitLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );

        jPanelSplit.add(jPanelSplitLeft);

        jPanelSplitRight.setName("jPanelSplitRight"); // NOI18N

        javax.swing.GroupLayout jPanelSplitRightLayout = new javax.swing.GroupLayout(jPanelSplitRight);
        jPanelSplitRight.setLayout(jPanelSplitRightLayout);
        jPanelSplitRightLayout.setHorizontalGroup(
            jPanelSplitRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelSplitRightLayout.setVerticalGroup(
            jPanelSplitRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );

        jPanelSplit.add(jPanelSplitRight);

        jSeparatorCenter.setName("jSeparatorCenter"); // NOI18N

        jCheckBoxHideMale.setText(resourceMap.getString("jCheckBoxHideMale.text")); // NOI18N
        jCheckBoxHideMale.setActionCommand(resourceMap.getString("jCheckBoxHideMale.actionCommand")); // NOI18N
        jCheckBoxHideMale.setName("jCheckBoxHideMale"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${config.hideMale}"), jCheckBoxHideMale, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBoxHideMale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxHideMaleActionPerformed(evt);
            }
        });

        jScrollPaneLeft.setName("jScrollPaneLeft"); // NOI18N

        jCheckBoxHideFemale.setText(resourceMap.getString("jCheckBoxHideFemale.text")); // NOI18N
        jCheckBoxHideFemale.setActionCommand(resourceMap.getString("jCheckBoxHideFemale.actionCommand")); // NOI18N
        jCheckBoxHideFemale.setName("jCheckBoxHideFemale"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${config.hideFemale}"), jCheckBoxHideFemale, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBoxHideFemale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxHideFemaleActionPerformed(evt);
            }
        });

        jScrollPaneRight.setName("jScrollPaneRight"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxShowSex)
                            .addComponent(jCheckBoxShowNames))
                        .addContainerGap(135, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBoxShowCorrections)
                        .addContainerGap(91, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBoxHideMale, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(jCheckBoxHideFemale))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPaneLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPaneRight, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)))
                        .addGap(6, 6, 6)
                        .addComponent(jPanelSplit, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparatorCenter, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                        .addGap(10, 10, 10))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPaneLeft, jScrollPaneRight});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jCheckBoxShowSex)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxShowNames)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxShowCorrections)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparatorCenter, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jPanelSplit, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBoxHideFemale)
                            .addComponent(jCheckBoxHideMale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPaneLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPaneRight, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxHideFemaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxHideFemaleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxHideFemaleActionPerformed

    private void jCheckBoxHideMaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxHideMaleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxHideMaleActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxHideFemale;
    private javax.swing.JCheckBox jCheckBoxHideMale;
    private javax.swing.JCheckBox jCheckBoxShowCorrections;
    private javax.swing.JCheckBox jCheckBoxShowNames;
    private javax.swing.JCheckBox jCheckBoxShowSex;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanelSplit;
    private javax.swing.JPanel jPanelSplitLeft;
    private javax.swing.JPanel jPanelSplitRight;
    private javax.swing.JScrollPane jScrollPaneLeft;
    private javax.swing.JScrollPane jScrollPaneRight;
    private javax.swing.JSeparator jSeparatorCenter;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public MorenoView2Face getConfig() {
        return config;
    }

    private void initComponentsManual() {
        // male selector
        final CheckBoxListTable selectMaleTable = new CheckBoxListTable(new CheckBoxListTable.Handler<MorenoRespondentFace>() {
            @Override
            public List<DataItem<MorenoRespondentFace>> provideData() {
                List<DataItem<MorenoRespondentFace>> result = new ArrayList();
                for (MorenoRespondentFace resp : MorenoApp.getModel().getRespondentsList()) {
                    if (resp.getSex() == MorenoSex.MALE) {
                        result.add(new DataItem<MorenoRespondentFace>(resp, (resp.getIndex()+1)+"."+resp.getName(), !config.isHideRespondent(resp)));
                    }
                }
                return result;
            }

            @Override
            public void applyChanges(DataItem<MorenoRespondentFace> aItem) {
                config.setHideRespondent(aItem.getId(), !aItem.isSelected());
            }
        });
        jScrollPaneLeft.setViewportView(selectMaleTable);

        // female selector
        final CheckBoxListTable selectFemaleTable = new CheckBoxListTable(new CheckBoxListTable.Handler<MorenoRespondentFace>() {
            @Override
            public List<DataItem<MorenoRespondentFace>> provideData() {
                List<DataItem<MorenoRespondentFace>> result = new ArrayList();
                for (MorenoRespondentFace resp : MorenoApp.getModel().getRespondentsList()) {
                    if (resp.getSex() == MorenoSex.FEMALE) {
                        result.add(new DataItem<MorenoRespondentFace>(resp, (resp.getIndex()+1)+"."+resp.getName(), !config.isHideRespondent(resp)));
                    }
                }
                return result;
            }

            @Override
            public void applyChanges(DataItem<MorenoRespondentFace> aItem) {
                config.setHideRespondent(aItem.getId(), !aItem.isSelected());
            }
        });
        jScrollPaneRight.setViewportView(selectFemaleTable);

        //add listeners
//        config.addRespondentsChangeListener(new MorenoViewChangeListener() {
//            public void onDisplayConfigChanged(String property, Object oldValue, Object newValue) {
//                if (MorenoRespondentsSociogramConfig.HIDE_RESPONDENT.equals(property)) {
//                    selectMaleTable.refreshData();
//                    selectFemaleTable.refreshData();
//                }
//            }
//        });
        MorenoApp.getModel().addModelListener(new MorenoModelAdapter() {
            @Override
            public void onRespondentChanged(MorenoRespondentFace changed) {
                selectMaleTable.refreshData();
                selectFemaleTable.refreshData();
            }

            @Override
            public void onRespondentsListChanged() {
                selectMaleTable.refreshData();
                selectFemaleTable.refreshData();
            }
        });
    }
}
